{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  王惠民\n",
    "+ 学号  0165192"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Legends never die\n",
      "Legends Never Die-2017英雄联盟全球总决赛主题曲\n",
      "When the world is calling you\n",
      "Can you hear them screaming out your name\n",
      "Legends never die\n",
      "They become a part of you\n",
      "Every time you bleed for reaching greatness\n",
      "Relentless you survive\n",
      "They never lose hope when everything's cold and the fighting's near\n",
      "Oh pick yourself up 'cause\n",
      "Legends never die\n",
      "When the world is calling you\n",
      "Can you hear them screaming out your name\n",
      "Legends never die\n",
      "They become a part of you\n",
      "Every time you bleed for reaching greatness\n",
      "Legends never die\n",
      "They're written down in eternity\n"
     ]
    }
   ],
   "source": [
    "def legend():\n",
    "    print(\"Legends never die\" )\n",
    "legend()\n",
    "print(\"Legends Never Die-2017英雄联盟全球总决赛主题曲\\nWhen the world is calling you\\nCan you hear them screaming out your name\")\n",
    "legend()\n",
    "print(\"They become a part of you\\nEvery time you bleed for reaching greatness\\nRelentless you survive\\nThey never lose hope when everything's cold and the fighting's near\\nOh pick yourself up 'cause\")\n",
    "legend()\n",
    "print(\"When the world is calling you\\nCan you hear them screaming out your name\")\n",
    "legend()\n",
    "print(\"They become a part of you\\nEvery time you bleed for reaching greatness\")\n",
    "legend()\n",
    "print(\"They're written down in eternity\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第一种方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "共输入了13个数\n",
      "均值:9.0\n",
      "样本方差:21.230769230769226\n",
      "共输入了12个数\n",
      "均值:5.583333333333333\n",
      "样本方差:9.076388888888893\n"
     ]
    }
   ],
   "source": [
    "def unvariable( *saber):\n",
    "    count=0\n",
    "    sum=0\n",
    "    for value in saber:\n",
    "        count+=1\n",
    "        sum+=value\n",
    "    ave=sum/count\n",
    "    print(\"共输入了%s个数\" % count)\n",
    "    print(\"均值:%s\" % ave)\n",
    "    sumsquare=0\n",
    "    for value in saber:\n",
    "        sumsquare+=value**2\n",
    "    var=sumsquare/count-ave**2\n",
    "    print(\"样本方差:%s\" % var)\n",
    "unvariable(9,2,3,4,5,8,8,8,15,10,16,15,14)\n",
    "unvariable(8,5,9,8,8,1,4,0,8,8,6,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "共输入13个数\n",
      "均值:5.923076923076923\n",
      "求方差,input 1\n",
      "求标准差,input 2\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def unvariable( *saber):\n",
    "    count=0\n",
    "    sum=0\n",
    "    for value in saber:\n",
    "        count+=1\n",
    "        sum+=value\n",
    "    ave=sum/count\n",
    "    print(\"共输入%s个数\" % count)\n",
    "    print(\"均值:%s\" % ave)\n",
    "    sumsquare=0\n",
    "    for value in saber:\n",
    "        sumsquare+=value**2\n",
    "    var=sumsquare/count-ave**2\n",
    "    print(\"求方差,input 1\")\n",
    "    print(\"求标准差,input 2\")\n",
    "    a=int(input())\n",
    "    if a==1:\n",
    "        print(\"样本方差:%s\" % var)\n",
    "    else:\n",
    "        varstd=math.sqrt(var)\n",
    "        print(\"样本标准差：%s\" %varstd )\n",
    "unvariable(1,1,2,3,4,0,6,7,8,9,10,11,15)\n",
    "unvariable(2,3,18,9,7,13,1,4,8,5,9,8,8,1,4,0,8,8,6,2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import math\n",
    "def unvariable( *saber):\n",
    "    count=0\n",
    "    sum=0\n",
    "    nsquare=0\n",
    "    for value in saber:\n",
    "        count+=1\n",
    "        sum+=value\n",
    "    ave=sum/count\n",
    "    print(\"共输入%s个数\" % count)\n",
    "    print(\"均值:%s\" % ave)\n",
    "    sumsquare=0\n",
    "    for value in saber:\n",
    "        sumsquare+=value**2\n",
    "    var=sumsquare/count-ave**2\n",
    "    print(\"求方差,input 1\")\n",
    "    print(\"求标准差,input 2\")\n",
    "    print(\"求最大值,最小值,均值标准误，input 3\")\n",
    "    a=int(input())\n",
    "    if a==1:\n",
    "        print(\"样本方差:%s\" % var)\n",
    "    if a==2:\n",
    "        varstd=math.sqrt(var)\n",
    "        print(\"样本标准差：%s\" %varstd )\n",
    "    if a==3:\n",
    "        for value in saber:\n",
    "            if value>max:\n",
    "                max=value\n",
    "        print(\"样本中最大值是：%s\" % max)\n",
    "        for value in saber:\n",
    "            if value<min:\n",
    "                min=value\n",
    "        print(\"样本中最小值是：%s\" % min)\n",
    "        nsquare=math.sqrt(count)\n",
    "        print(\"均值标准误为：%s\" % (var/nsquare))  \n",
    "statistics(1,1,1,2,2,3,4,5)\n",
    "statistics(0,1,2,5,11,9,6,7)\n",
    "statistics(0,0,0,4,6,8)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
